#!/bin/bash
repo_root='/jenkins_home/'

set +ue
if [ -z "${repo_url}" ];then
    repo_url='https://gitee.com/src-openeuler/manifest.git'
fi
set -ue

git config --global credential.helper store
git config --global user.name peulerosci

function repo_config()
{
    if [ ! -d ".repo" ]; then
        rm -rf init_repo_euleros
        curl -O http://dgggit09-rd.huawei.com/static/init_repo_euleros
        chmod 755 init_repo_euleros
        expect -c "
        set timeout -1
        spawn ./init_repo_euleros peulerosci
        expect {
            \"*(yes/no)*\" { send \"yes\r\"; exp_continue }
            \"*Username*\" { send \"peulerosci\r\"; exp_continue }
            \"*Password*\" { send \"EulerOS_123\r\"; exp_continue }
            eof { catch wait result; exit [lindex \$result 3] }
        }
        expect {
            eof { catch wait result; exit [lindex \$result 3] }
        }
"
        rm -f init_repo_euleros
    fi
}

function repo_init()
{
    manifest=$1
    expect -c "
        set timeout -1
        spawn repo init -u $repo_url -b master -m $manifest --no-repo-verify --repo-branch=euleros  --no-clone-bundle
        expect {
            \"*(yes/no)*\" { send \"yes\r\"; exp_continue }
            \"*Username*\" { send \"peulerosci\r\"; exp_continue }
            \"*Password*\" { send \"EulerOS_123\r\"; exp_continue }
            eof { catch wait result; exit [lindex \$result 3] }
        }
        expect {
            eof { catch wait result; exit [lindex \$result 3] }
        }
"
    if [ $? -ne 0 ]; then
        repo init -u $repo_url -b master -m $manifest --no-repo-verify --repo-branch=euleros  --no-clone-bundle
    fi
}


function repo_sync()
{
    PROJECT=$1
	local num=0 
	local Max=3 
    repo_clean $PROJECT
    expect -c "
        set timeout -1
        spawn repo sync $PROJECT --no-clone-bundle --no-tags 
        expect {
            \"*(yes/no)*\" { send \"yes\r\"; exp_continue }
            \"*Username*\" { send \"peulerosci\r\"; exp_continue }
            \"*Password*\" { send \"EulerOS_123\r\"; exp_continue }
            eof { catch wait result; exit [lindex \$result 3] }
        }
        expect {
            eof { catch wait result; exit [lindex \$result 3] }
        }
"
    if [ $? -ne 0 ]; then
		repo sync $PROJECT --no-clone-bundle --no-tags &> /tmp/repo_sync.log 
    fi
}

function repo_sync_dir()
{
    Directory=$1
        local num=0
        local Max=3
    repo_clean
    expect -c "
        set timeout -1
        spawn repo sync -r $Directory --no-clone-bundle --no-tags
        expect {
            \"*(yes/no)*\" { send \"yes\r\"; exp_continue }
            \"*Username*\" { send \"peulerosci\r\"; exp_continue }
            \"*Password*\" { send \"EulerOS_123\r\"; exp_continue }
            eof { catch wait result; exit [lindex \$result 3] }
        }
        expect {
            eof { catch wait result; exit [lindex \$result 3] }
        }
"
    if [ $? -ne 0 ]; then
                repo sync $Directory --no-clone-bundle --no-tags &> /tmp/repo_sync.log
    fi
}

function repo_sync_all()
{
    cnt=`lscpu | grep '^CPU(s):' | awk '{print $2}'`
    let cnt=cnt/2-1
    repo_clean
    expect -c "
        set timeout -1
        spawn repo sync -c -j$cnt --no-clone-bundle --no-tags
        expect {
            \"*(yes/no)*\" { send \"yes\r\"; exp_continue }
            \"*Username*\" { send \"peulerosci\r\"; exp_continue }
            \"*Password*\" { send \"EulerOS_123\r\"; exp_continue }
            eof { catch wait result; exit [lindex \$result 3] }
        }
        expect {
            eof { catch wait result; exit [lindex \$result 3] }
        }
"
    if [ $? -ne 0 ]; then
        repo forall -c "git clean -df && git reset --hard"
        repo sync -c -j$cnt --no-clone-bundle --no-tags
    fi
}

function repo_clean() {
    set +e
    local prjs=$@
    expect -c "
        set timeout -1
        spawn repo forall $prjs -c \"git clean -df && git reset --hard\"
        expect {
            \"*(yes/no)*\" { send \"yes\r\"; exp_continue }
            \"*Username*\" { send \"peulerosci\r\"; exp_continue }
            \"*Password*\" { send \"EulerOS_123\r\"; exp_continue }
            eof { catch wait result; exit [lindex \$result 3] }
        }
        expect {
            eof { catch wait result; exit [lindex \$result 3] }
        }
"
    return 0
    set -e
}

function repo_create()
{
    dir_name=$1
    manifest=$2
    if [ ! -d $repo_root/$dir_name/.repo ]; then
        rm -rf $repo_root/$dir_name
        mkdir -p $repo_root/$dir_name
        cd $repo_root/$dir_name
        repo_config
        repo_init $manifest
        cd - &> /dev/null
    fi
}

function repo_download()
{
    project=$1
    change_numer=$2
    patchset_numer=$3
	result=1
	set +e
    repo_clean $project
    repo_sync $project
    expect -c "
    set timeout -1
    spawn repo download $project $change_numer/$patchset_numer
        expect {
            \"*(yes/no)*\" { send \"yes\r\"; exp_continue }
            \"*Username*\" { send \"peulerosci\r\"; exp_continue }
            \"*Password*\" { send \"EulerOS_123\r\"; exp_continue }
            eof { catch wait result; exit [lindex \$result 3] }
        }
        expect {
            eof { catch wait result; exit [lindex \$result 3] }
        }
"
	#解决ACK/NAK 问题
    if [ $? -ne 0 ]; then
		for((i=0;i<3;i++)) 
		do 
			repo download $project $change_numer/$patchset_numer
			if [ $? -ne 0 ]; then
				continue
			else
				result=0 
				break
			fi
		done
		if [ $result -eq 0 ]; then
			set -e
			return 0 
		else
			exit 1 
		fi 
    fi
}

download_dir=$1
if ! which repo &> /dev/null;then
    repo_config
fi
if [ ! -d .repo ];then
    repo_init EulerOS_next_docker.xml
fi
repo_sync_dir $download_dir
